package com.three.lab.page.message;



import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.snackbar.Snackbar;
import com.three.lab.R;
import com.three.lab.adapter.CommentAdapter;
import com.three.lab.adapter.InteractMsgAdapter;
import com.three.lab.base.AppConstant;
import com.three.lab.base.BaseActivity;
import com.three.lab.bean.CommentBean;
import com.three.lab.bean.InteractMsgBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 互动消息
 * created by lqd on 2024-07-09.
 */
public class InteractMsgDetailActivity extends BaseActivity {

    private TextView tvContent,tvTitle,tvTime;
    private ImageView ivAvatar,ivLike,ivCollect;
    private EditText etInput;
    private Button btnSend;
    private InteractMsgBean bean = null;

    private TextView tvLoading;
    private View mLoadingView;
    private ImageView ivState;
    private ProgressBar progressBar;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private CommentAdapter adapter;
    private int pageNo = 1;
    private boolean isloadMore = false;

    @Override
    protected void initParms(Bundle bundle) {
        if (bundle != null) {
            bean = bundle.getParcelable(AppConstant.EXTRAS_DATA);
        }
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_interact_msg_detail;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        tvTitle = $(R.id.tv_title);
        tvTitle.setText(getString(R.string.interact_msg));

        tvContent = $(R.id.tv_content);
        tvTime = $(R.id.tv_time);
        ivAvatar = $(R.id.iv_cover);
        ivLike = $(R.id.iv_like);
        ivCollect = $(R.id.iv_collect);
        etInput = $(R.id.et_input);
        btnSend = $(R.id.btn_send);

        refreshLayout = $(R.id.refresh_layout);
        mLoadingView = LayoutInflater.from(this).inflate(R.layout.loading_view, null);
        ivState = mLoadingView.findViewById(R.id.iv_state);
        tvLoading = mLoadingView.findViewById(R.id.tv_loading);
        progressBar = mLoadingView.findViewById(R.id.progressBar);
        recyclerView = $(R.id.rv_comment);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new CommentAdapter();
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void setListener() {
        $(R.id.iv_left).setOnClickListener(this);
        $(R.id.iv_like).setOnClickListener(this);
        $(R.id.iv_collect).setOnClickListener(this);
        $(R.id.btn_send).setOnClickListener(this);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onRefreshing();
            }
        });
        tvLoading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onRefreshing();
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (isloadMore) {
                            pageNo++;
                            loadData();
                        }
                    }
                }, 1000);
            }
        }, recyclerView);

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                CommentBean bean = (CommentBean) baseQuickAdapter.getItem(position);
                switch (view.getId()){
                    case R.id.iv_good:
                        onOperateGood(view,bean,position);
                        break;
                    case R.id.iv_bad:
                        onOperateBad(view,bean,position);
                        break;
                }
            }
        });

        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String text = s.toString();
                if (TextUtils.isEmpty(text)) {
                    btnSend.setVisibility(View.GONE);
                    ivCollect.setVisibility(View.VISIBLE);
                    ivLike.setVisibility(View.VISIBLE);
                } else {
                    ivLike.setVisibility(View.GONE);
                    ivCollect.setVisibility(View.GONE);
                    btnSend.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void onOperateGood(View view,CommentBean bean,int position) {
        if (bean.isGood()) {
            bean.setGood(false);
        } else {
            bean.setGood(true);
        }
        adapter.notifyItemChanged(position);
        Snackbar.make(view, getString(R.string.operate_success), Snackbar.LENGTH_LONG).show();
    }

    private void onOperateBad(View view,CommentBean bean,int position) {
        if (bean.isBad()) {
            bean.setBad(false);
        } else {
            bean.setBad(true);
        }
        adapter.notifyItemChanged(position);
        Snackbar.make(view, getString(R.string.operate_success), Snackbar.LENGTH_LONG).show();
    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()){
            case R.id.iv_left:
                finish();
                break;
            case R.id.iv_like:
                if (bean != null) {
                    if (bean.isLike()) {
                        bean.setLike(false);
                        ivLike.setImageResource(R.mipmap.ic_like_black_64);
                    } else {
                        bean.setLike(true);
                        ivLike.setImageResource(R.mipmap.ic_like_bred_64);
                    }
                }
                break;
            case R.id.iv_collect:
                if (bean != null) {
                    if (bean.isCollect()) {
                        bean.setCollect(false);
                        ivCollect.setImageResource(R.mipmap.ic_collect_nor);
                    } else {
                        bean.setCollect(true);
                        ivCollect.setImageResource(R.mipmap.ic_collect_red);
                    }
                }
                break;
            case R.id.btn_send:
                onSendComment(v);
                break;
        }
    }

    private void onSendComment(View view) {
        String text = etInput.getText().toString();
        showLoading(getString(R.string.submitting));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                hideLoading();
                btnSend.setVisibility(View.GONE);
                ivCollect.setVisibility(View.VISIBLE);
                ivLike.setVisibility(View.VISIBLE);
                etInput.setText("");
                Snackbar.make(view, getString(R.string.delete_success), Snackbar.LENGTH_LONG).show();
                onRefreshing();
            }
        },500);
    }

    @Override
    protected void initData(Context mContext) {
        if (bean != null) {
            tvContent.setText(bean.getConent());
            tvTime.setText(bean.getTime());
        }
        onRefreshing();
    }

    private void onRefreshing() {
        progressBar.setVisibility(View.VISIBLE);
        tvLoading.setText(getString(R.string.loading));
        ivState.setVisibility(View.GONE);
        adapter.setEnableLoadMore(true);
        adapter.getData().clear();
        if (refreshLayout != null && refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(true);
        }
        pageNo = 1;
        isloadMore = false;
        adapter.notifyDataSetChanged();
        loadData();
    }

    private void loadData() {
        adapter.setEmptyView(mLoadingView);

        List<CommentBean> mlist = new ArrayList<>();
        for (int i = 0; i < 28; i++) {
            CommentBean bean = new CommentBean();
            bean.setBad(false);
            bean.setContent("很好，非常好，不错，非常不错，哈哈哈，你好呀，今天晚上吃什么呢？");
            bean.setId(i);
            bean.setTime("2024-07-12 14:28:46");
            bean.setUserName("张三" + i);
            mlist.add(bean);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                onResetRefresh();
                setListAdapter(mlist);//设置页面数据
            }
        }, 1000);
    }

    private void setListAdapter(List<CommentBean> mlist) {
        if (isloadMore) {
            adapter.addData(mlist);
            if (mlist.size() < AppConstant.DEFAULT_PAGE_SIZE) {
                adapter.loadMoreEnd();
            } else {
                adapter.loadMoreComplete();
                adapter.setEnableLoadMore(true);
            }
        } else if (mlist.isEmpty()) {
            adapter.loadMoreEnd();
            adapter.setEnableLoadMore(false);
            setState(getString(R.string.no_data), R.mipmap.ic_empty);
        } else {
            adapter.setNewData(mlist);
            adapter.loadMoreComplete();
            adapter.setEnableLoadMore(true);

            isloadMore = false;
            adapter.loadMoreEnd();
        }
    }

    private void setState(String string, int resId) {
        progressBar.setVisibility(View.GONE);
        tvLoading.setText(string);
        ivState.setImageResource(resId);
        ivState.setVisibility(View.VISIBLE);
        adapter.setEmptyView(mLoadingView);
        adapter.notifyDataSetChanged();
    }

    private void onResetRefresh() {
        progressBar.setVisibility(View.GONE);
        if (refreshLayout.isRefreshing()) {
            refreshLayout.setRefreshing(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onResetRefresh();
    }

}